package com.imooc.newretail.bpl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.newretail.common.utils.BeanUtils;
import com.imooc.newretail.bpl.dto.MenuDTO;
import com.imooc.newretail.bpl.dto.MenuTableTreeDTO;
import com.imooc.newretail.bpl.entity.Menu;
import com.imooc.newretail.bpl.mapper.MenuMapper;
import com.imooc.newretail.bpl.service.IMenuService;
import com.imooc.newretail.bpl.dto.MenuTreeDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @program:new-retail-teach
 * @description:菜单业务服务实现类
 * @author:Hank.He
 * @create:2020-06-25 10:34
 **/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {


    @Override
    public List<MenuTreeDTO> getMenuTree(String menuRootId) {
        List<Menu> menus = getBaseMapper().findAl();
        List<MenuTreeDTO> menuTreeList = new ArrayList<>();
        //遍历构建菜单树
        //首先找出没有上级的菜单作为菜单根
        for (Menu menu : menus) {
            if (StringUtils.isEmpty(menu.getParentId())) {
                //然后遍历每个菜单ID的子菜单集合
                MenuTreeDTO menuTreeDTO = new MenuTreeDTO();
                BeanUtils.copyProperties(menu, menuTreeDTO);
                menuTreeList.add(menuTreeDTO);
                loadMenuClients(menuTreeDTO, menus);
            }
        }
        return menuTreeList;
    }

    public List<MenuTableTreeDTO> getMenuTableTree(String menuRootId) {
        List<Menu> menus = getBaseMapper().findAl();
        List<MenuTableTreeDTO> menuTreeList = new ArrayList<>();
        //遍历构建菜单树
        //首先找出没有上级的菜单作为菜单根
        for (Menu menu : menus) {
            if (StringUtils.isEmpty(menu.getParentId())) {
                //然后遍历每个菜单ID的子菜单集合
                MenuTableTreeDTO menuTableTree = new MenuTableTreeDTO();
                BeanUtils.copyProperties(menu, menuTableTree);
                menuTreeList.add(menuTableTree);
                loadMenuClients(menuTableTree, menus);
            }
        }
        return menuTreeList;
    }

    @Override
    @Transactional
    public int importMenu(List<MenuTreeDTO> list) {
        int row = 0;
        List<Menu> menuList = new ArrayList<>();
        convert(list, menuList, null);
        for (Menu menu : menuList) {
            getBaseMapper().insert(menu);
            row++;
        }
        return row;
    }

    public List<MenuDTO> listAll() {
        QueryWrapper query = new QueryWrapper();
        query.orderByAsc("parent_id", "show_order");
        List<Menu> menus = getBaseMapper().selectList(query);
//        List<SysMenu> menus = baseMapper.findAl(query);

        return BeanUtils.copyPropertiesForList(menus, MenuDTO.class);
    }

    private void convert(List<MenuTreeDTO> list, List<Menu> menuList, String parentId) {
        for (int i = 0; i < list.size(); i++) {
            MenuTreeDTO tmenu = list.get(i);
//        for (MenuTreeDTO tmenu : list) {
            Menu menu = BeanUtils.copyProperties(tmenu, Menu.class);
            menu.setMenuId(getUUID());
            if (!StringUtils.isEmpty(parentId)) {
                menu.setParentId(parentId);
            }
            menu.setShowOrder(i + 1);
            String menuId = menu.getMenuId();
            convert(tmenu.getChild(), menuList, menuId);
            menuList.add(menu);
        }
    }


    private String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

//    public PageInfo<MenuDTO> getList(PageParam param) {
//        Query query = param.toQuery();
//        PageInfo pageInfo = MapperUtil.query(baseMapper, query);
//        List<MenuDTO> dtolist = BeanUtils.copyPropertiesForList(pageInfo.getList(), MenuDTO.class);
//        pageInfo.setList(dtolist);
//        return pageInfo;
//    }

//    @Override
//    @Transactional()
//    public int saveMenu(MenuDTO menu) {
//        Menu m = new Menu();
//        //先进行对象转换
//        BeanUtils.copyProperties(menu, m);
//        if (StringUtils.isEmpty(m.getMenuId())) {
//            int row = baseMapper.insert(m);
//            System.out.print(m.toString());
//            return row;
//        } else {
//            return baseMapper.updateById(m);
//        }
//    }

    @Override
    public int removeById(String menuId) {
        return getBaseMapper().deleteById(menuId);
    }


    /***
     * 递归分析菜单树
     * @param menuRoot
     * @param menus
     */
    private void loadMenuClients(MenuTreeDTO menuRoot, List<Menu> menus) {
        for (Menu menu : menus) {
            if (StringUtils.endsWithIgnoreCase(menuRoot.getMenuId(), menu.getParentId())) {
                MenuTreeDTO menuTreeDTO = new MenuTreeDTO();
                BeanUtils.copyProperties(menu, menuTreeDTO);
                menuRoot.addChild(menuTreeDTO);
                loadMenuClients(menuTreeDTO, menus);
            }
        }
    }

}
